package com.hustzp.com.xichuangzhu.poetry;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XichuangzhuApplication;
import com.hustzp.com.xichuangzhu.dao.WorksDao;
import com.hustzp.com.xichuangzhu.fragment.DayFragment;
import com.hustzp.com.xichuangzhu.me.MyBaseActivity;
import com.hustzp.com.xichuangzhu.poetry.model.LikePost;
import com.hustzp.com.xichuangzhu.poetry.model.PostComment;
import com.hustzp.com.xichuangzhu.poetry.model.Works;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

/**
 * Created by Administrator on 2016/6/9.
 */
public class PostCommentActivity extends MyBaseActivity {

    private EditText etCmtContent;

    private LikePost mLikePost;
    private PostComment mPostCmt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_comment);

        mLikePost = getIntent().getParcelableExtra(LikePost.class.getSimpleName());
        mPostCmt = getIntent().getParcelableExtra(PostComment.class.getSimpleName());

        initToolbar();
        initViews();
    }

    private void initToolbar() {
        TextView titleTxt = (TextView) findViewById(R.id.tv_title_txt);
        if (mPostCmt != null) {
            titleTxt.setText("回复" + mPostCmt.getUSER().getUsername() + "的评论");
        }


        ImageView closeViewBtn = (ImageView) findViewById(R.id.img_close_view);
        closeViewBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 保存草稿
                if (!TextUtils.isEmpty(etCmtContent.getText().toString().trim())) {
                    XichuangzhuApplication.getInstance().saveCommentDraft(etCmtContent.getText().toString());
                    showToastInfo("已保存评论草稿");
                }
                finish();
            }
        });

        TextView postCmtBtn = (TextView) findViewById(R.id.tv_post_comment);
        postCmtBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 提交评论
                if (TextUtils.isEmpty(etCmtContent.getText().toString().trim())) {
                    DialogFactory.hintDialog(PostCommentActivity.this, "评论内容不可为空");
                    return;
                }

                AVObject postCmtObj = new AVObject("PostComment");
                postCmtObj.put("post", mLikePost);
                postCmtObj.put("user", AVUser.getCurrentUser());
                postCmtObj.put("content", etCmtContent.getText().toString());

                if (mPostCmt != null) {
                    postCmtObj.put("targetUser", mPostCmt.getUSER());
                    postCmtObj.put("targetComment", mPostCmt);
                }

                postCmtObj.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            showToastInfo("评论成功");
                            setResult(RESULT_OK);
                            finish();
                        } else {
                            DialogFactory.hintDialog(PostCommentActivity.this, "提交评论失败，请重试");
                        }
                    }
                });

            }
        });
    }

    private void initViews() {
        etCmtContent = (EditText) findViewById(R.id.et_comment_content);

        // 读取评论草稿
        if (!XichuangzhuApplication.getInstance().getCommentDraft().isEmpty()) {
            etCmtContent.setText(XichuangzhuApplication.getInstance().getCommentDraft());
        }

        TextView postTxt = (TextView) findViewById(R.id.tv_post_txt);
        WorksDao worksDao = new WorksDao(PostCommentActivity.this);
        Works mWorks = worksDao.getWorksById(mLikePost.getWORKID());
        postTxt.setText(mWorks.getAuthor() + "<<" + mWorks.getTitle() + ">>");

        // 查看详情
        RelativeLayout poetryDetailLayout = (RelativeLayout) findViewById(R.id.poetry_detail_layout);
        poetryDetailLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 作品详情
                Intent intent = new Intent(PostCommentActivity.this, PoetryDetailAct.class);
                intent.putExtra("from", DayFragment.class.getSimpleName());
                intent.putExtra(Works.class.getSimpleName(), mLikePost.getWorks());
                startActivity(intent);
            }
        });
    }
}
